package gases.as3.librarys.components.ui.control.treeclasses
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import gases.as3.librarys.components.events.TreeEvent;
	import gases.as3.librarys.components.skin.data.BaseSkin;
	import gases.as3.librarys.components.skin.data.TreeSkin;
	import gases.as3.librarys.components.ui.GUIComponents;
	import gases.as3.librarys.components.ui.control.Tree;
	
	/**
	 *树 字节点 
	 * @author gases
	 * 
	 */	
	public class TreeCellRenderer extends GUIComponents 
	{
		protected var mouseArea:Sprite;
		protected var bg:Sprite;
		protected var icon:*;
		protected var paddingX:int = 10;
		protected var childrenContainer:Sprite;
		protected var openIcon:*;
		protected var closeIcon:*;
		protected var labelTextField:TextField;
		
		private var _data:TreeCellRendererVO;
		private var _parentNode:TreeCellRenderer;
		private var _childNodes:Array;
		private var _hasParentNode:Boolean;
		private var _hasChildNodes:Boolean;
		private var _level:int;
		private var _isSelected:Boolean;
		private var _maxMaskX:Number;
		private var _maskHeight:Number;
		private var _maskX:Number;
		
		private var _openBm:Bitmap;
		private var _closeBm:Bitmap;
		
		public var nodeStatus:String = Tree.NODE_CLOSE;
		
		public function TreeCellRenderer()
		{
			super();
		}
		
		override protected function initialization():void
		{
			super.initialization();
			this.buttonMode = true;
			this.initUI();
			this.initListeners();
		}
		
		override public function dispose():void
		{
			removeListener();
			super.dispose();
		}
		
		public function setNodeSkin(value:BaseSkin):void
		{
			var treeSkin:TreeSkin = value as TreeSkin;
			_closeBm = new Bitmap(treeSkin.close_icon as BitmapData);
			_openBm = new Bitmap(treeSkin.open_icon as BitmapData);
		}
		
		/**
		 *更新
		 * @param _width
		 *
		 */
		public function updateRenderer(_maxMaskX1:Number):void
		{
			this._maskX = (this._level * 10) * -1;
			this._maxMaskX = _maxMaskX1;
			var mmx:Number = _maxMaskX1;
			this.setMouseArea(_maskX, mmx);
			this.setBg(_maskX, mmx);
		}

		/**
		 *选中
		 *
		 */
		public function selected():void
		{
			this._isSelected = true;
			this.setSelectedBg();
		}
		
		/**
		 *没选中
		 *
		 */
		public function unSelected():void
		{
			this._isSelected = false;
			this.setMouseOutBg();
		}
		
		/**
		 *是否打开 
		 * @return 
		 * 
		 */	
		public function isOpen():Boolean 
		{
			if (!this._hasChildNodes) 
			{
				return false;
			}
			if (this.nodeStatus == Tree.NODE_OPEN)
			{
				return true;
			}
			if (this.nodeStatus == Tree.NODE_CLOSE)
			{
				return false;
			}
			return false;
		}
		
		/**
		 *绘制子节点显示对象
		 *
		 */
		public function drawChildrenDisplay():void 
		{
			this.childrenContainer = new Sprite();
			
			for (var i:int = 0; i < this._childNodes.length; i++) 
			{
				var childDis:TreeCellRenderer = this._childNodes[i];
				this.childrenContainer.addChild(childDis);
			}
			this.drawLayout();
		}
		
		protected function otherClickNode(e:TreeEvent):void
		{
			this.drawLayout();
		}
		
		/**
		 *移入鼠标
		 * @param e
		 *
		 */
		protected function onMouseOver(e:MouseEvent):void
		{
			e.stopPropagation();
			
			if (!this._isSelected)
			{
				this.setMouseOverBg();
			}
		}
		
		/**
		 *移出鼠标
		 * @param e
		 *
		 */
		protected function onMouseOut(e:MouseEvent):void
		{
			e.stopPropagation();
			
			if (!this._isSelected)
			{
				this.setMouseOutBg();
			}
		}
		
		/**
		 *开始解析数据
		 *
		 */
		protected function resloveData():void 
		{
			this.drawData();
		}
		
		/**
		 *根据数据 构建UI
		 */
		protected function drawData():void
		{
			if(this.hasChildNodes)
			{
				this.addChild(_closeBm);
				_closeBm.y = 5;
			}
			this.labelTextField.tabEnabled = false;
			this.labelTextField.multiline = false;
			this.labelTextField.selectable = false;
			this.labelTextField.autoSize = TextFieldAutoSize.LEFT;
			var textFormat:TextFormat = new TextFormat();
			textFormat.color = 0x000000;
			this.labelTextField.defaultTextFormat = textFormat;
			this.labelTextField.text = this.data.label;
			this.labelTextField.x = _closeBm.width + 5;
			this.labelTextField.y = 0;
			this.addChild(this.labelTextField);
			
			this.addChild(this.mouseArea);
			//设置下遮罩的高度
			this._maskHeight = Math.max(this.labelTextField.height, _closeBm.height);
		}
	
		/**
		 *设置位置
		 *
		 */
		protected function drawLayout():void
		{
			if (!this._hasChildNodes) 
			{
				return ;
			}
			
			for (var i:int = 0; i < this._childNodes.length; i++)
			{
				var childDis:TreeCellRenderer = this._childNodes[i];
				
				if (i == 0)
				{
					childDis.y = 0;
				} else 
				{
					childDis.y = this._childNodes[i - 1].y + this._childNodes[i - 1].height;
				}
			}
		}
		
		public function get level():int
		{
			return _level;
		}
		
		public function set level(value:int):void
		{
			_level = value;
			
			if (value != 0)
			{
				this.x = paddingX;
			}
		}
		
		/**
		 *是否有子节点
		 */
		public function get hasChildNodes():Boolean
		{
			return _hasChildNodes;
		}
		
		/**
		 *是否有父节点
		 */
		public function get hasParentNode():Boolean
		{
			return _hasParentNode;
		}
		
		/**
		 *子节点的数据 Array<TreeCellRenderer>
		 */
		public function get childNodes():Array
		{
			return _childNodes;
		}
		
		public function set childNodes(value:Array):void
		{
			_childNodes = value;
			this._hasChildNodes = true;
		}
		
		/**
		 *父节点的数据
		 */
		public function get parentNode():TreeCellRenderer
		{
			return _parentNode;
		}
		
		public function set parentNode(value:TreeCellRenderer):void
		{
			_parentNode = value;
			
			if (_parentNode != null) 
			{
				this._hasParentNode = true;
			}
		}
		
		public function get data():TreeCellRendererVO 
		{
			return _data;
		}
		
		public function set data(value:TreeCellRendererVO):void 
		{
			_data = value;
			if (_data != null) 
			{
				this.resloveData();
			}
		}
		
		/**
		 *初始化UI
		 */
		protected function initUI():void 
		{
			this.bg = new Sprite();
			this.addChild(this.bg);
			
			this.mouseArea = new Sprite();
			
			this.icon = new MovieClip();
			this.labelTextField = new TextField();
			
     		}
		
		protected function setBg(_x:Number, _width:Number):void
		{
			this.bg.graphics.clear();
			this.bg.graphics.beginFill(0x000000, 0);
			this.bg.graphics.drawRect(_x, 0, _width, _maskHeight);
			this.bg.graphics.endFill();
		}
		
		protected function setMouseArea(_x:Number, w:Number):void
		{
			this.mouseArea.graphics.clear();
			this.mouseArea.graphics.beginFill(0x000000, 0);
			this.mouseArea.graphics.drawRect(_x, 0, w, _maskHeight);
			this.mouseArea.graphics.endFill();
		}
		
		protected function setMouseOverBg():void 
		{
			this.bg.graphics.clear();
			this.bg.graphics.beginFill(0x000000, .2);
			this.bg.graphics.drawRect(_maskX, 0, this._maxMaskX, _maskHeight);
			this.bg.graphics.endFill();
		}
		
		protected function setMouseOutBg():void 
		{
			this.bg.graphics.clear();
		}
		
		protected function setSelectedBg():void
		{
			this.bg.graphics.clear();
			this.bg.graphics.beginFill(0x000000, .5);
			this.bg.graphics.drawRect(_maskX, 0, this._maxMaskX, _maskHeight);
			this.bg.graphics.endFill();
		}
		
		/**
		 *增加监听 
		 * 
		 */		
		protected function initListeners():void
		{
			this.addEventListener(TreeEvent.CLICK_NODE, otherClickNode);
			this.addEventListener(MouseEvent.CLICK, onClick);
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		protected function removeListener():void 
		{
			this.removeEventListener(TreeEvent.CLICK_NODE, otherClickNode);
			this.removeEventListener(MouseEvent.CLICK, onClick);
			this.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		/**
		 *点击事件
		 * @param e
		 *
		 */
		protected function onClick(e:MouseEvent):void 
		{
			//终止事件的冒泡，因为MouseEvent事件在FP底层是默认冒泡派发的
			e.stopPropagation();
			this.clickNode();
			this.dispatchEvent(new TreeEvent(TreeEvent.CLICK_NODE,this));
		}
		
		/**
		 *打开节点操作
		 *
		 */
		protected function open():void 
		{
			if (this.hasChildNodes) 
			{
				if (this.isOpen() == false) 
				{
					this.setOpenIcon();
					this.nodeStatus = Tree.NODE_OPEN;
					this.childrenContainer.y = this.height;
					this.addChild(this.childrenContainer);
				}
			}
		}
		
		/**
		 *关闭节点操作
		 *
		 */
		protected function close():void 
		{
			if (this.hasChildNodes) 
			{
				if (this.isOpen() == true)
				{
					this.setCloseIcon();
					this.nodeStatus = Tree.NODE_CLOSE;
					this.removeChild(this.childrenContainer);
				}
			}
		}
		
		/**
		 *是否选中
		 * @return Boolean
		 *
		 */
		protected function get isSelected():Boolean
		{
			return _isSelected;
		}
		
		/**
		 *节点打开的ICON
		 *
		 */
		protected function setOpenIcon():void
		{
			if(this.contains(_openBm))
			{
				this.removeChild(_openBm);
			}
			if(this.contains(_closeBm))
			{
				this.removeChild(_closeBm);
			}
			this.addChild(_openBm);
			_openBm.y = 5;
		}
		
		/**
		 *节点关闭的ICON
		 *
		 */
		protected function setCloseIcon():void 
		{
			if(this.contains(_openBm))
			{
				this.removeChild(_openBm);
			}
			if(this.contains(_closeBm))
			{
				this.removeChild(_closeBm);
			}
			this.addChild(_closeBm);
			_closeBm.y = 5;
		}
		
		protected function clickNode():void 
		{
			this.selected();
			if (!this.isOpen()) 
			{
				this.open();
			} else 
			{
				this.close();
			}
		}
	}
}